package elearning.record.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.transaction.annotation.Transactional;

import elearning.record.dao.BrowseDao;
import elearning.record.entity.Browse;
import elearning.user.entity.User;
import elearning.video.entity.Video;

@Transactional
public class BrowseService {

	private BrowseDao browseDao;

	public void setBrowseDao(BrowseDao browseDao) {
		this.browseDao = browseDao;
	}

	public void save(Integer uid, Integer videoid, String videoDot) {
		Browse  browse = browseDao.findByUidAndVideoId(uid,videoid);
		if(browse!=null){
			
			browse.setIfWatch(1);
			browse.setVideoDot(videoDot);
			SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm");
			Date date = new Date();
			browse.setBrowseDate(f.format(date));
			browseDao.update(browse);
		}else{
			browse = new Browse();
			User user = new User();
			Video video = new Video();
			user.setUserId(uid);
			video.setVideoId(videoid);
			browse.setUser(user);
			browse.setVideo(video);
			browse.setIfWatch(1);
			browse.setVideoDot(videoDot);
			SimpleDateFormat f = new SimpleDateFormat("yyyy/MM/dd HH:mm");
			Date date = new Date();
			browse.setBrowseDate(f.format(date));
			browseDao.save(browse);
		}
	}


}
